package cn.jinjimi.app.store.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.store.DaoSession;
import cn.jinjimi.app.store.entity.custom.LastFollow;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.internal.SqlUtils;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.

/**
 * DAO for table "LAST_FOLLOW".
 */
public class LastFollowDao extends AbstractDao<LastFollow, Long> {

    public static final String TABLENAME = "LAST_FOLLOW";

    /**
     * Properties of entity LastFollow.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property FollowId = new Property(0, Long.class, "followId", true, "FOLLOW_ID");
        public final static Property Content = new Property(1, String.class, "content", false, "CONTENT");
        public final static Property FollowTime = new Property(2, Long.class, "followTime", false, "FOLLOW_TIME");
        public final static Property CustomId = new Property(3, Long.class, "customId", false, "CUSTOM_ID");
    }

    ;

    private DaoSession daoSession;


    public LastFollowDao(DaoConfig config) {
        super(config);
    }

    public LastFollowDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /**
     * Creates the underlying database table.
     */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "\"LAST_FOLLOW\" (" + //
                "\"FOLLOW_ID\" INTEGER PRIMARY KEY ," + // 0: followId
                "\"CONTENT\" TEXT," + // 1: content
                "\"FOLLOW_TIME\" INTEGER," + // 2: followTime
                "\"CUSTOM_ID\" INTEGER);"); // 3: customId
    }

    /**
     * Drops the underlying database table.
     */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"LAST_FOLLOW\"";
        db.execSQL(sql);
    }

    /**
     * @inheritdoc
     */
    @Override
    protected void bindValues(SQLiteStatement stmt, LastFollow entity) {
        stmt.clearBindings();

        Long followId = entity.getFollowId();
        if (followId != null) {
            stmt.bindLong(1, followId);
        }

        String content = entity.getContent();
        if (content != null) {
            stmt.bindString(2, content);
        }

        Long followTime = entity.getFollowTime();
        if (followTime != null) {
            stmt.bindLong(3, followTime);
        }

        Long customId = entity.getCustomId();
        if (customId != null) {
            stmt.bindLong(4, customId);
        }
    }

    @Override
    protected void attachEntity(LastFollow entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /**
     * @inheritdoc
     */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    /**
     * @inheritdoc
     */
    @Override
    public LastFollow readEntity(Cursor cursor, int offset) {
        LastFollow entity = new LastFollow( //
                cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // followId
                cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // content
                cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // followTime
                cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3) // customId
        );
        return entity;
    }

    /**
     * @inheritdoc
     */
    @Override
    public void readEntity(Cursor cursor, LastFollow entity, int offset) {
        entity.setFollowId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setContent(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setFollowTime(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setCustomId(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
    }

    /**
     * @inheritdoc
     */
    @Override
    protected Long updateKeyAfterInsert(LastFollow entity, long rowId) {
        entity.setFollowId(rowId);
        return rowId;
    }

    /**
     * @inheritdoc
     */
    @Override
    public Long getKey(LastFollow entity) {
        if (entity != null) {
            return entity.getFollowId();
        } else {
            return null;
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getCustomerDao().getAllColumns());
            builder.append(" FROM LAST_FOLLOW T");
            builder.append(" LEFT JOIN CUSTOM T0 ON T.\"CUSTOM_ID\"=T0.\"CUSTOMER_ID\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }

    protected LastFollow loadCurrentDeep(Cursor cursor, boolean lock) {
        LastFollow entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Custom custom = loadCurrentOther(daoSession.getCustomerDao(), cursor, offset);
        entity.setCustom(custom);

        return entity;
    }

    public LastFollow loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();

        String[] keyArray = new String[]{key.toString()};
        Cursor cursor = db.rawQuery(sql, keyArray);

        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }

    /**
     * Reads all available rows from the given cursor and returns a list of new ImageTO objects.
     */
    public List<LastFollow> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<LastFollow> list = new ArrayList<LastFollow>(count);

        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }

    protected List<LastFollow> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }


    /**
     * A raw-style query where you can pass any WHERE clause and arguments.
     */
    public List<LastFollow> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }

    public void clearLastFollowByCustomId(long customId) {
        queryBuilder().where(Properties.CustomId.eq(customId)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

}
